<?php

namespace app\beautiful\controller\admin\mall\store;

use app\BaseController;

use app\common\logic\DataLogic;

use app\beautiful\logic\LogsLogic;

use app\beautiful\logic\printer\PrinterLogic;

use app\beautiful\model\member\MemberModel;

use app\beautiful\model\order\StoreOrderModel;

use think\facade\Db;



class Order extends BaseController{

    public function index()

    {

        $order_id = $this->request->param('order_id/d',0);

        $mobile=$this->request->param('mobile');

        $status=$this->request->param('status/d',0);



        $where=[

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];



        $member_where=[

            ['shop_id','=',$this->request->shopId]

        ];

        if(!empty($mobile)){

            $member_where[]=['mobile','=',$mobile];

        }

        $member_ids= MemberModel::where($member_where)->column('member_id') ?? [];

        $where[]=['member_id','in',$member_ids];



        if(!empty($order_id)) $where[]=['store_order_id','=',$order_id];

        if(!empty($status)) $where[]=['status','=',$status];



        $store_order_model=new StoreOrderModel();

        $store_order_model=$store_order_model->with(['member']);



        $order=['add_time'=>'desc'];

        $field="*";

        $append=['add_time_format','pay_time_format','status_means','pay_type_mean','gift_total_price'];



        $return = DataLogic::getDataList($store_order_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);



        $write_off_num=StoreOrderModel::where([

                ['shop_id','=',$this->request->shopId],

                ['is_delete','=',0],

                ['status','=',4]

            ])->count() ?? 0;

        $return['write_off_num']=$write_off_num;

        //处理数据
        $lists = $return['list'];
        if(empty(!$lists)){

            $key_start = $this->request->limit * ($this->request->page - 1);
            $key_end = $return['total'];

            foreach ($lists as $k => $list){
                $lists[$k]['show_store_order_id']= $key_end - ($key_start + $k);
            }
        }
        $return['list']=$lists;

        return $this->success($return);



    }



    public function detail()

    {

        $store_order_id = $this->request->param('store_order_id/d',0);

        if($store_order_id==0) return $this->error('订单不存在#1');

        if(null === $store_order = StoreOrderModel::find($store_order_id)) return $this->error('订单不存在#2');

        if($store_order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');

        if($store_order->is_delete != 0) return $this->error('订单不存在#4');



        if(null === $member = MemberModel::find($store_order->member_id)) return $this->error('订单不存在#5');

        if($member->shop_id != $this->request->shopId) return $this->error('订单不存在#6');



        $store_order->append(['add_time_format','pay_time_format','status_means','pay_type_mean','gift_total_price','remain_num']);



        $detail=[

            'store_order_id'=>$store_order->store_order_id,

            'add_time_format'=>$store_order->add_time_format,

            'status'=>$store_order->status,

            'status_means'=>$store_order->status_means,

            'need_pay'=>$store_order->need_pay,

            'pay_type'=>$store_order->pay_type,

            'pay_type_mean'=>$store_order->pay_type_mean,

            'pay_time_format'=>$store_order->pay_time_format,

            'goods_total_price'=>$store_order->goods_total_price,

            'youhui_balance'=>$store_order->youhui_balance,

            'member_name'=>$member->nick_name,

            'member_mobile'=>$member->mobile,

            'goods_name'=>$store_order->goods_name,

            'goods_cover_img'=>$store_order->goods_cover_img,

            'goods_num'=>$store_order->goods_num,

            'remain_num'=>$store_order->remain_num,

            'goods_gift_giving'=>$store_order->goods_gift_giving,

            'gift_total_price'=>$store_order->gift_total_price,

        ];



        return $this->success(['detail'=>$detail]);

    }



    public function writeOffStoreOrder()

    {

        $store_order_id = $this->request->param('store_order_id/d',0);

        $num = $this->request->param('num/d',0);

        $remarks=$this->request->param('remarks');

        if($store_order_id==0) return $this->error('订单不存在#1');

        if($num <= 0) return $this->error('请选择核销数量');





        if(null === $store_order = StoreOrderModel::find($store_order_id)) return $this->error('订单不存在#2');

        if($store_order->shop_id != $this->request->shopId) return $this->error('订单不存在#3');

        if($store_order->is_delete != 0) return $this->error('订单不存在#4');

        if($store_order->status !=4 ) return $this->error('订单无法进行核销');

        $write_off_num=$store_order->write_off_num + $num;

        if($write_off_num > $store_order->goods_num) return $this->error('核销数量不够');



        if(null === $member = MemberModel::find($store_order->member_id)) return $this->error('订单不存在#5');

        if($member->shop_id != $this->request->shopId) return $this->error('订单不存在#6');



        Db::startTrans();

        try{

            //订单操作

           $store_order->write_off_num=$write_off_num;

            if($write_off_num >= $store_order->goods_num) $store_order->status=8;

           $store_order->save();



           //插入一条消费记录

            $remain_num=$store_order->goods_num-$store_order->write_off_num;

           $res=LogsLogic::storeOrder($this->request->shopId,$member->member_id,3,$store_order_id,$num,$remain_num,$remarks,$this->request->accountId);



            //打印机

            $printer_logic =new PrinterLogic($this->request->shopId);

            $printer_logic->print('storeOrderWriteOff',$res);

           Db::commit();

           return $this->success('核销成功');



        }catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }

}

